package com.project.schoolFoodShare.controller.school;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.domain.FoodList;
import com.project.schoolFoodShare.domain.FoodRestaurant;
import com.project.schoolFoodShare.domain.FoodSchool;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.service.FoodListService;
import com.project.schoolFoodShare.service.FoodRestaurantService;
import com.project.schoolFoodShare.service.FoodSchoolService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @description: 学校controller
 */
@Controller
@ResponseBody
@RequestMapping("school")
public class FoodSchoolController {

    @Autowired
    private FoodSchoolService foodSchoolService;
    @Autowired
    private FoodRestaurantService foodRestaurantService;
    @Autowired
    private FoodListService foodListService;

    /** 分页获取学校 */
    @PostMapping("getFoodSchoolPage")
    public Result getFoodSchoolPage(@RequestBody FoodSchool foodSchool) {
        Page<FoodSchool> page = new Page<>(foodSchool.getPageNumber(),foodSchool.getPageSize());
        QueryWrapper<FoodSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(foodSchool.getName()),FoodSchool::getName,foodSchool.getName());
        Page<FoodSchool> foodSchoolPage = foodSchoolService.page(page, queryWrapper);
        return Result.success(foodSchoolPage);
    }

    @GetMapping("getFoodSchoolList")
    public Result getFoodSchoolList() {
        List<FoodSchool> schoolList = foodSchoolService.list();
        return Result.success(schoolList);
    }

    /** 根据id获取学校 */
    @GetMapping("getFoodSchoolById")
    public Result getFoodSchoolById(@RequestParam("id")String id) {
        FoodSchool foodSchool = foodSchoolService.getById(id);
        return Result.success(foodSchool);
    }

    /** 保存学校 */
    @PostMapping("saveFoodSchool")
    public Result saveFoodSchool(@RequestBody FoodSchool foodSchool) {
        boolean save = foodSchoolService.save(foodSchool);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑学校 */
    @PostMapping("editFoodSchool")
    public Result editFoodSchool(@RequestBody FoodSchool foodSchool) {
        boolean save = foodSchoolService.updateById(foodSchool);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除学校 */
    @GetMapping("removeFoodSchool")
    public Result removeFoodSchool(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                FoodSchool foodSchool = foodSchoolService.getById(id);
                //删除之前有没有餐厅和分享
                QueryWrapper<FoodRestaurant> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(FoodRestaurant::getSchool,foodSchool.getName());
                int count = foodRestaurantService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("当前学校存在餐厅,无法删除");
                }
                QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(FoodList::getSchoolId,id);
                int counted = foodListService.count(wrapper);
                if (counted > 0) {
                    return Result.fail("当前学校存在美食分享,无法删除");
                }
                foodSchoolService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("学校id不能为空！");
        }
    }

}
